import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import VueLazyLoad from 'vue-lazyload'
import VueCookie from 'vue-cookie'
import {
  Message
} from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import router from './router.js'
import store from './store/index'

/**
 * 根据前端的跨域方式做基础值的调整
 * baseURL : 基本URL  /a/b : /api/a/b => /a/b
 * timeout : 请求超时
 * mock    : mock 数据的开关
 */

const mock = false
if (mock) {
  require('./mock/api')
}
axios.defaults.baseURL = '/api'
axios.defaults.timeout = 8000

// 拦截代码 错误返回拦截
axios.interceptors.response.use(function (response) {
  let res = response.data
  let path = location.hash
  if (res.status == 0) {
    return res.data;
  } else if (res.status == 10) {
    if (path != '#/index') {
      window.location.href = '/#/login'
    }
    return Promise.reject(res);
  } else {
    // alert(res.msg)
    Message.warning(res.msg);
    return Promise.reject(res);
  }
}, (error) => {
  let res = error.response;
  Message.error(res.data.message);
  return Promise.reject(error);
})

// 挂载 axios，可以 this 调用
Vue.use(VueAxios, axios)
// 挂载 Vue-cookie
Vue.use(VueCookie)
// 挂载 VueLazyLoad 懒加载插件
Vue.use(VueLazyLoad, {
  loading: '/imgs/loading-svg/loading-bars.svg'
})
// 原型添加 ElementUi
Vue.prototype.$message = Message
// 生产环境提示
Vue.config.productionTip = false

new Vue({
  render: h => h(App),
  router,
  store
}).$mount('#app')